<template>
	<view class="page">
		<u-navbar :placeholder="true" title="用户对比" @leftClick="$utils.back()" border />
		<view class="viewWidth iconBox">
			<view @click='openCalendar'>
				<u-icon name="calendar" size="30rpx" label='日历' labelSize='24rpx'></u-icon>
			</view>
			<u-icon name="attach" size="30rpx" label='轨道' labelSize='24rpx'></u-icon>
		</view>
		<view class="body">
			<z-paging ref="paging" style="height: 100%;" :fixed="false" v-model="List" @query="query"
				:default-page-no="1" :default-page-size="10">
				<view class="imageBox viewWidth">
					<template v-if="!loading">
						<view v-for="(item,index) in List" :key="index" class="imageItem">
							<view class="time">第{{getTimeDifferenceInDays(item.created_at,new Date())}}天</view>
							<image :src="item.url" mode="aspectFill"></image>
						</view>
					</template>
					<template v-else>
						<view v-for="(item,index) in 10" :key="index" class="imageItem Skeleton">
						</view>
					</template>
				</view>
			</z-paging>
		</view>

		<root-portal>
			<u-calendar :maxDate='maxDate' :minDate='minDate()' :show="calendarShow" @close='calendarShow=false'
				@confirm='confirm'></u-calendar>
		</root-portal>
	</view>

</template>

<script>
	export default {
		data() {
			return {
				show: false,
				List: [],
				loading: true,
				user_id: null,
				conquer_id: null,
				calendarShow: false,
				date: null,
				maxDate: 0,
			}
		},
		onLoad(op) {
			setTimeout(() => {
				this.maxDate = this.maxDatefun()
			}, 1000)
			this.user_id = op.id
			this.conquer_id = op.conquer_id
		},
		methods: {
			openCalendar() {
				this.calendarShow = true
			},
			minDate() {
				// 获取当前时间
				var currentDate = new Date();

				// 获取一个月前的时间
				currentDate.setMonth(currentDate.getMonth() - 2);

				// 获取年、月、日
				var year = currentDate.getFullYear();
				var month = currentDate.getMonth() + 1; // 月份从0开始，需要加1
				var day = currentDate.getDate();

				// 格式化输出
				var formattedDate = year + "-" + month + "-" + day;
				console.log(formattedDate)
				return formattedDate
			},
			maxDatefun() {
				// 创建一个表示当前时间的Date对象
				var today = new Date();

				// 使用getDate()方法获取当前日期，并加上1得到明天的日期
				var tomorrowDate = today.getDate();

				// 使用setDate()方法设置Date对象的日期为明天的日期
				today.setDate(tomorrowDate);

				// 获取明天的年份、月份和日期
				var tomorrowYear = today.getFullYear();
				var tomorrowMonth = today.getMonth() + 1; // 月份从0开始，所以要加1
				var tomorrowDay = today.getDate();


				return tomorrowYear + "-" + tomorrowMonth + "-" + tomorrowDay
			},
			confirm(e) {
				let date = e[0]
				this.date = date.split('-').join('/')
				this.$refs.paging.reload()
				this.calendarShow = false
			},
			getTimeDifferenceInDays(timestamp1, timestamp2) {
				// 将时间戳转换为日期对象
				const date1 = new Date(timestamp1);
				const date2 = new Date(timestamp2);

				// 计算两个日期之间的毫秒差
				const timeDifferenceInMilliseconds = Math.abs(date2 - date1);

				// 将毫秒转换为天数，一天有 24 * 60 * 60 * 1000 毫秒
				const oneDayInMilliseconds = 24 * 60 * 60 * 1000;
				const differenceInDays = Math.ceil(timeDifferenceInMilliseconds / oneDayInMilliseconds);

				return differenceInDays - 1;
			},
			query(page, size) {
				let obj = {
					page,
					size
				}
				if (this.date) {
					obj.date = this.date
				}
				uni.showLoading({
					title: '加载中...'
				})
				this.$api.User.Images(this.user_id, this.conquer_id, obj).then(res => {
					this.loading = false
					let arr = []
					res.data.activities.data.forEach(item => {
						arr.push(...item.images)
					})
					this.$refs.paging.complete(arr)
					uni.hideLoading()
				}).catch(err => {
					uni.hideLoading()
				})

			}
		}
	}
</script>

<style lang="scss" scoped>
	.page {
		height: 100vh;
		padding-bottom: calc(constant(safe-area-inset-bottom) + 10rpx);
		/* 兼容 iOS 设备 */
		padding-bottom: calc(env(safe-area-inset-bottom) + 10rpx);
		box-sizing: border-box;
		display: flex;
		flex-direction: column;
	}

	.body {
		flex: 1;
		margin-top: 20rpx;
	}

	.imageBox {
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;

		>.imageItem {
			width: 345rpx;
			height: 345rpx;
			overflow: hidden;
			margin-bottom: 20rpx;
			border-radius: 10rpx;
			/* box-sizing: 0 0 10rpx #000; */
			box-shadow: 0 0 10rpx #bbb;
			position: relative;
		}

		image {
			width: 100%;
			height: 100%;
		}

		.time {
			background: rgba(0, 0, 0, 0.3);
			padding: 10rpx 20rpx;
			border-radius: 8rpx;
			color: #fff;
			position: absolute;
			display: flex;
			align-items: center;
			justify-content: center;
			top: 20rpx;
			left: 20rpx;
		}
	}

	.iconBox {
		display: flex;
		justify-content: flex-end;
		margin-top: 20rpx;
	}
</style>